package gui;

import ge.escola.Serie;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListModel;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CadastraSeriePanel extends javax.swing.JPanel {
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JButton buttonCadastrar;
	private JComboBox comboNome1;
	private JButton buttonCancelar;
	private JComboBox comboNome2;

	private String nome1,nome2;
	
	public CadastraSeriePanel() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(800, 600));
			this.setBackground(new java.awt.Color(204,204,255));
			this.setLayout(null);
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Cadastra Serie");
				jLabel1.setBounds(242, 28, 310, 49);
				jLabel1.setFont(new java.awt.Font("Tahoma",0,48));
				jLabel1.setForeground(new java.awt.Color(0,102,102));
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Nome serie");
				jLabel2.setBounds(71, 199, 123, 40);
				jLabel2.setFont(new java.awt.Font("Tahoma",0,24));
				jLabel2.setForeground(new java.awt.Color(0,102,102));
			}
			{
				ComboBoxModel comboBoxModel = 
					new DefaultComboBoxModel(new String[] { "E.F. (Ensino Fundamental)", "E.M. (Ensino Medio)" });
				comboNome2 = new JComboBox();
				this.add(comboNome2);
				comboNome2.setModel(comboBoxModel);
				comboNome2.setBounds(254, 208, 241, 27);
				comboNome2.setBackground(new java.awt.Color(255,255,255));
				comboNome2.setFont(new java.awt.Font("Tahoma",0,18));
				comboNome2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						comboNome2ActionPerformed(evt);
					}
				});
			}
			{
				buttonCadastrar = new JButton();
				this.add(buttonCadastrar);
				buttonCadastrar.setText("CADASTRAR");
				buttonCadastrar.setBounds(440, 530, 112, 25);
				buttonCadastrar.setBackground(new java.awt.Color(255,255,255));
				buttonCadastrar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							buttonCadastrarMouseClicked(evt);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
			{
				buttonCancelar = new JButton();
				this.add(buttonCancelar);
				buttonCancelar.setText("CANCELAR");
				buttonCancelar.setBackground(new java.awt.Color(255,255,255));
				buttonCancelar.setBounds(597, 530, 112, 25);
				buttonCancelar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							buttonCancelarMouseClicked(evt);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
			{
				ComboBoxModel comboNome1Model = 
					new DefaultComboBoxModel(
							new String[] { "1", "2","3","4","5","6","7","8","9" });
				comboNome1 = new JComboBox();
				this.add(comboNome1);
				comboNome1.setModel(comboNome1Model);
				comboNome1.setBounds(206, 208, 42, 27);
				comboNome1.setBackground(new java.awt.Color(255,255,255));
				comboNome1.setFont(new java.awt.Font("Tahoma",0,18));
				comboNome1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						comboNome1ActionPerformed(evt);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void comboNome2ActionPerformed(ActionEvent evt) {
		nome2 = (String) comboNome2.getSelectedItem();
		nome2 = nome2.substring(0, 4);
	}
	
	private void comboNome1ActionPerformed(ActionEvent evt) {
		nome1 = (String) comboNome1.getSelectedItem();
	}
	
	private void buttonCadastrarMouseClicked(MouseEvent evt) throws Exception {
		String nome = nome1 + " " + nome2;
		Serie serie = new Serie(nome);
		if(GerenciadorDeJanelas.todasSeries.contains(serie)){
			JOptionPane.showMessageDialog(null, "Essa serie ja existe.");
		}else{
			GerenciadorDeJanelas.todasSeries.add(serie);
			
			String xml = GerenciadorDeJanelas.ARQUIVADOR.toXML(GerenciadorDeJanelas.todasSeries);
			GerenciadorDeJanelas.gravar(GerenciadorDeJanelas.arquivoDeSeries,xml);
			
			GerenciadorDeJanelas.window.setVisible(false);
	        GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.menuCadastra);
	        GerenciadorDeJanelas.window.setVisible(true);
		}
	}
	
	private void buttonCancelarMouseClicked(MouseEvent evt) throws Exception {
		GerenciadorDeJanelas.window.setVisible(false);
        GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.menuCadastra);
        GerenciadorDeJanelas.window.setVisible(true);
	}

}
